<!--
 * @Description: 设施分组管理页面
-->
<template>
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar
        :page-no="102"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :has-edit="false"
        :has-save="false"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm
        :formConfig="formConfig"
        :formData="searchlist"
        @search="search"
      ></SearchForm>
    </template>
    <template v-slot:table-box>
      <Table
        :propList="tableColumn"
        v-loading="loading"
        :listData="tableData"
        @handleSelectionChange="handleSelectionChange"
      >
        <template v-slot:facility="{ row }">
          <template v-if="row.facilityGroupNo">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="seeEquip(row)"
            >
              <i class="el-icon-view"></i> 查看
            </el-button>
          </template>
        </template>
        <template v-slot:operation="{ row }">
          <template v-if="!row.index && row.facilityGroupNo">
            <el-button
              v-if="judgePermission(102, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              style="height: 15px"
              @click="handleEdit(row, 'edit')"
              ><i class="el-icon-edit-outline"></i>修 改</el-button
            >
            <el-button
              v-if="judgePermission(102, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(row)"
              ><i class="el-icon-delete"></i> 删除</el-button
            >
          </template>
        </template>
      </Table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '设施分组'"
        :visible.sync="dialogFormVisible"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="分组名称:" prop="facilityGroupName" class="w100p">
            <el-input v-model="formData.facilityGroupName" placeholder="" clearable />
          </el-form-item>
          <el-form-item label="设施选择:" prop="facilityNoList" class="w100p">
            <el-select
              v-model="formData.facilityNoList"
              placeholder="请选择设施"
              filterable
              clearable
              multiple
              collapse-tags
            >
              <el-option
                v-for="item in facilityList"
                :key="item.facilityNo"
                :label="item.facilityName"
                :value="item.facilityNo"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <SeeEquip
        :is-show-equip="isShowEquip"
        :equip-data="facilityBOList"
        @closeDialog="isShowEquip = false"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import facilityApi from "@/api/basic-info-manage/facility-group-management";
import SearchForm from "@/components/searchForm";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import Table from '@/components/table/table.vue';
import SeeEquip from "@/components/seeEquip/index.vue";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";
import Axios from "axios";
export default {
  // eslint-disable-next-line vue/name-property-casing
  name: "dataManagement",
  components: { Pagination, BaseMainSlot, SearchForm, ActionBar, Table, SeeEquip },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: "分组名称",
          prop: "facilityGroupName",
          type: "input"
        }
      ],
      searchlist: {},
      facilityList: [],
      rules: {
        facilityGroupName: [{ required: true, message: "分组名称不能为空", trigger: "blur" }],
      },
      tableColumn: [
        { prop: 'facilityGroupName', label: '分组名称', minWidth: '200' },
        { prop: 'facilityNoList', label: '关联设施', slotName: 'facility', minWidth: '150' },
        { prop: 'operation', label: '操作', slotName: 'operation', width: "200" }
      ],
      tableData: [],
      loading: false,
      // 选中的id数组 ID组成
      idList: [],
      dialogFormVisible: false,
      deviceName: "",
      isShowEquip: false,
      facilityBOList: [],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.queryFGByFtNo()
    // 获取表格数据
    this.getTableDataList();
  },
  mounted() {},
  methods: {
    queryFGByFtNo () {
      facilityApi.queryFGByFtNo().then(res => {
        let { data } = res
        console.log(data)
        let list = []
        let facilityGroupList = data.facilityGroupList
        let facilityList = data.facilityList
        facilityGroupList.map(el => {
          el.facilityList.map(ele => {
            list.push(ele)
          })
        })
        facilityList.map(el => {
          list.push(el)
        })
        this.facilityList = list
      })
    },
    // 获取表格数据
    getTableDataList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          ...this.searchlist,
          farmingTypeNo: sessionStorage.farmingTypeNo,
          organizationNo: sessionStorage.organizationNo,
        }
      };
      this.loading = true;
      facilityApi.queryPageByCondition(params).then(res => {
        this.loading = false;
        let data = res.data;
        if (data) {
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      }).catch(() => {
        this.loading = false;
      });
    },
    dateFormat(row, colnum) {
      return this.$moment(row[colnum.property]).format("YYYY-MM-DD");
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    async handleEdit(row) {
      // TODO 点击行元素后 edit
      const { data } = await facilityApi.queryFacilityGroup(
        row.facilityGroupNo
      );
      let facilityNoList = data.map(el => { return el.facilityNo })
      this.formData = { ...row, facilityNoList };
      this.dialogFormVisible = true
      this.nowChanceStau = "edit"
    },
    handleSelectionChange(list) {
      this.idList = [];
      this.idList = list.map(item => item.facilityGroupNo);
      if (this.idList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      facilityApi.delList({ noList: this.idList }).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || "删除失败");
        }
      });
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      }).then(() => {
        facilityApi.delByNo(row.facilityGroupNo).then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.getTableDataList();
          } else {
            this.$message.warning(res.msg || "删除失败");
          }
        });
      })
      .catch(() => {
        this.$message("取消删除");
      });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            // 巡检点数组赋值进formData
            facilityApi.edit(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                // this.postId = "";
                this.getTableDataList();
                this.$refs[formData].resetFields();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            facilityApi.add(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                // this.postId = "";
                this.getTableDataList();
                this.$refs[formData].resetFields();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 查看设施
    async seeEquip(row) {
      const { data: res } = await facilityApi.queryFacilityGroup(
        row.facilityGroupNo
      );
      if (res && res.length) {
        this.isShowEquip = true;
        this.facilityBOList = res;
      } else {
        this.$message.warning("暂无设施");
      }
    },
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";

</style>
